package com.taoshen.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description
 * @Author Zhang Litao
 * @Date 2021/12/20 9:40
 */
@RestController
@RequestMapping("captcha")
public class CaptchaController {
    /**
     * 获取验证码
     */
    @GetMapping("getCaptcha")
    public void getCaptcha(HttpServletResponse response) throws IOException {
        // 生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        System.out.println(lineCaptcha.getCode());

        // 将验证码写入response的流
        ServletOutputStream outputStream = response.getOutputStream();
        lineCaptcha.write(outputStream);
        outputStream.close();
    }
}
